package com.example.demo.converter;

import com.example.demo.dao.Post;
import com.example.demo.dto.PostDTO;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class PostConverter {
    public static PostDTO convertPost(Post post)
    {
        PostDTO postDTO = new PostDTO();
        postDTO.setContent(post.getContent());
        postDTO.setImageBytes(post.getImageBytes());
        postDTO.setTitle(post.getTitle());
        postDTO.setCreatedAt(post.getCreatedAt());
        postDTO.setImageName(post.getImageName());
        postDTO.setUserDTO(UserConverter.convertUser(post.getUser()));
        postDTO.setLikesCount(post.getLikesCount());
        postDTO.setCommentsCount(post.getCommentsCount());
        postDTO.setFavoritesCount(post.getFavoritesCount());
        postDTO.setSharesCount(post.getSharesCount());
        return postDTO;
    }
    public static Post convertPost(PostDTO postDTO)
    {
        Post post = new Post();
        post.setContent(postDTO.getContent());
        post.setCreatedAt(postDTO.getCreatedAt());
        post.setImageBytes(postDTO.getImageBytes());
        post.setImageName(postDTO.getImageName());
        post.setTitle(postDTO.getTitle());
        post.setUser(UserConverter.convertUser(postDTO.getUserDTO()));
        post.setLikesCount(postDTO.getLikesCount());
        post.setCommentsCount(postDTO.getCommentsCount());
        post.setFavoritesCount(postDTO.getFavoritesCount());
        post.setSharesCount(postDTO.getSharesCount());
        return post;
    }

    public static Set<PostDTO> convertToPostDTOSet(Set<Post> posts)
    {
        return posts.stream().map(PostConverter::convertPost).collect(Collectors.toSet());
    }
    public static Set<Post> convertToPostSet(Set<PostDTO> posts)
    {
        return posts.stream().map(PostConverter::convertPost).collect(Collectors.toSet());
    }
    public static List<PostDTO> convertPostListToPostDTOList(List<Post> postList) {
        return postList.stream()
                .map(PostConverter::convertPost)
                .collect(Collectors.toList());
    }

    public static List<Post> convertPostDTOListToPostList(List<PostDTO> postDTOList) {
        return postDTOList.stream()
                .map(PostConverter::convertPost)
                .collect(Collectors.toList());
    }
}
